package com.imooc.pojo.BO;

import com.imooc.exceptions.GraceException;
import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.pojo.AR.AdminAR;
import com.imooc.utils.MD5Utils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.Objects;

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class ResetPwdBO {

    private String adminId;
    private String password;
    private String rePassword;

    public void modifyPwd() {
        if (StringUtils.isBlank(adminId)) GraceException.display(ResponseStatusEnum.ADMIN_NAME_NULL_ERROR);
        if (StringUtils.isBlank(password)) GraceException.display(ResponseStatusEnum.ADMIN_PASSWORD_NULL_ERROR);
        if (!Objects.equals(password, rePassword)) GraceException.display(ResponseStatusEnum.ADMIN_PASSWORD_ERROR);

        AdminAR adminAR = new AdminAR();
        adminAR.setId(adminId);

        adminAR = adminAR.selectById();

        // 生成随机盐
        String slat = (int) ((Math.random() * 9 + 1) * 100000) + "";
        String pwd = MD5Utils.encrypt(password, slat);

        adminAR.setPassword(pwd);
        adminAR.setSlat(slat);

        adminAR.setUpdatedTime(LocalDateTime.now());
        adminAR.setCreateTime(LocalDateTime.now());

        adminAR.updateById();
    }
}
